<template>
  <div class="app-container">
    <label class="title"
           style="font-size: 20px;color: rgba(0,0,0,.85);font-weight: 600;line-height: 32px">设备信息</label>
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="关键字" prop="main">
        <el-input
          v-model="queryParams.main"
          placeholder="输入关键字查询"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="类型" prop="type">
        <el-select v-model="queryParams.type" placeholder="请选择设备类型" clearable size="small" multiple>
          <el-option
            v-for="dict in dict.type.deviceType"
            :key="dict.value"
            :label="dict.raw.remark"
            :value="dict.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="infoList">
      <el-table-column label="主键" align="center" prop="id"/>
      <el-table-column label="设备ID" align="center" prop="uid"/>
      <el-table-column label="设备名称" align="center" prop="name"/>
      <el-table-column label="设备编号" align="center" prop="code"/>
<!--      <el-table-column label="人脸识别激活码" align="center" prop="faceRecognitionCode"/>-->
      <el-table-column label="设备类型" align="center" prop="type">
        <template slot-scope="scope">
          <dict-tag-remark :options="dict.type.deviceType" :value="scope.row.type"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >修改
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNo"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" label-width="120px">
        <el-form-item label="设备ID" prop="uid">
          <el-input v-model="form.uid" class="rule-input-edit" disabled/>
        </el-form-item>
        <el-form-item label="设备名称" prop="name">
          <el-input v-model="form.name" class="rule-input-edit"/>
        </el-form-item>
        <el-form-item label="设备编号" prop="code">
          <el-input v-model="form.code" class="rule-input-edit"/>
        </el-form-item>
        <el-form-item label="人脸识别激活码" prop="faceRecognitionCode">
          <el-input v-model="form.faceRecognitionCode" class="rule-input-edit"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {listInfo, updateDeviceInfos, queryById} from "@/api/business/deviceInfo/deviceInfo";

export default {
  name: "Info",
  dicts: ['deviceType', 'isOnline'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 设备表格数据
      infoList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        main: "",
        type: "",
        tags: "",
        pageNo: 1,
        pageSize: 10,
      },
      // 表单参数
      form: {},
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询设备列表 */
    getList() {
      this.loading = true;
      let prefix = "?pageNo=" + this.queryParams.pageNo + "&pageSize=" + this.queryParams.pageSize + "&main=" + this.queryParams.main + "&tags=" + this.queryParams.tags;
      let value = "";
      for (const member of this.queryParams.type) {
        value = value + "&type=" + member;
      }
      let params = prefix + value;
      console.log(params);
      listInfo(params).then(response => {
        const records = response.data.records;
        this.infoList = records;
        for (var i = 0; i <= records.length; i = i + 1) {
          if (records[i] == null) {
            break
          }
          this.infoList[i].online = records[i].online + '';
        }
        this.total = response.data.totalCount;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNo = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    cancel() {
      this.open = false
      this.form = {}
      this.getList()
    },
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          const data = {
            id: this.form.id,
            code: this.form.code,
            name: this.form.name,
            faceRecognitionCode: this.form.faceRecognitionCode,
          }
          updateDeviceInfos(data).then(response => {
            if (response.status === 'success') {
              this.$modal.msgSuccess('修改成功')
              this.open = false
              this.getList()
            } else {
              this.$modal.msgSuccess(response.message)
            }
          })
        }
      })
    },
    handleUpdate(row) {
      this.open = true;
      this.form = JSON.parse(JSON.stringify(row));
    }
  }
};
</script>
